package com.zeng.sys.authmd.restfulmodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.nicecore.validation.ValidGroup;
import com.zeng.sys.authmd.apimodule.service.RolePermissionService;
import com.zeng.sys.authmd.apimodule.service.RoleService;
import com.zeng.sys.authmd.apimodule.vo.RoleVo;
import com.zeng.sys.authmd.restfulmodule.interceptor.AuthInterceptor;
import com.zeng.sys.authmd.servicemodule.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    RolePermissionService rolePermissionService;

    @RequestMapping("/list")
    public Object list(Page page, RoleVo roleVo) {
        List<RoleVo> page1 = this.roleService.findPage(page, roleVo);

        ArrayList<Map<String, Object>> objects = new ArrayList<>();
        page1.forEach(e -> {
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
//                    .put("updateDate", e.getUpdateDate())
//                    .put("insertDate", e.getInsertDate())
//                    .put("id", e.getId())
                    .put("name", e.getName())
                    .put("available", e.getAvailable())
                    .put(Role.Fields.roleConfig, e.getRoleConfig())
                    .put(Role.Fields.detailInfo, e.getDetailInfo())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("role", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
    public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("role", roleService.detailRoleVoAndPermission(code));

    }

    @RequestMapping("/add")
    public Object add(@RequestBody @Validated(ValidGroup.add.class) RoleVo roleVo) {
        rolePermissionService.addRoleAndInitRolePermission(roleVo);
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping("/update")
    public Object update(@RequestBody @Validated(ValidGroup.add.class) RoleVo roleVo) {
        rolePermissionService.updateRoleAndInitRolePermission(roleVo);
        AuthInterceptor.clearAllUserApi();
        return ResponseUtil.success("更改权限成功");
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody @NotNull String code) {
        return ResponseUtil.success(this.roleService.delete(code));
    }

    @RequestMapping("/batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        Integer integer = this.roleService.batchDelete(codes);
        return ResponseUtil.success(integer);
    }
}

